package com.example.yichuangyun.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.res.Resources;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.example.yichuangyun.R;
import com.example.yichuangyun.utils.DensityUtil;

import java.util.List;

public abstract class SinglePickerView {

    private OptionsPickerView<String> pickerView = null;
    private final List<String> data;
    private final String title;
    private boolean isDialog;

    public SinglePickerView(Activity activity, String title, List<String> data) {
        this.data = data;
        this.title = title;
        showPickerView(activity);
    }

    public SinglePickerView(Activity activity, String title, List<String> data, boolean isDialog) {
        this.data = data;
        this.title = title;
        this.isDialog = isDialog;
        showPickerView(activity);
    }

    private void showPickerView(Activity activity) {
        if (pickerView == null) {
            pickerView = new OptionsPickerBuilder(activity, (options1, option2, options3, v) -> {
                if (data != null && data.size() > 0) {
                    confirm(data.get(options1), options1);
                }
                pickerView.dismiss();
            }).addOnCancelClickListener(view -> cancel())
                    .setContentTextSize(20)
                    .setLayoutRes(R.layout.pickerview_custom_options, v -> {
                        //自定义布局中的控件初始化及事件处理
                        final TextView tvSubmit = v.findViewById(R.id.tv_finish);
                        final TextView tv_cancel = v.findViewById(R.id.tv_cancel);
                        final TextView tv_title = v.findViewById(R.id.tv_title);
                        tv_title.setText(title);
                        tvSubmit.setOnClickListener(v1 -> pickerView.returnData());
                        tv_cancel.setOnClickListener(v12 -> pickerView.dismiss());
                    })
                    .isDialog(isDialog)
                    .build();
            pickerView.setPicker(data);
            if (isDialog) {
                Dialog mDialog = pickerView.getDialog();
                if (mDialog != null) {
                    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) pickerView.getDialogContainerLayout().getLayoutParams();
                    params.leftMargin = 0;
                    params.rightMargin = 0;
                    pickerView.getDialogContainerLayout().setLayoutParams(params);
                    Window dialogWindow = mDialog.getWindow();
                    if (dialogWindow != null) {
                        // 设置弹框显示最顶层
                        dialogWindow.setType(WindowManager.LayoutParams.TYPE_APPLICATION);
                        WindowManager.LayoutParams attributes = dialogWindow.getAttributes();
                        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
                        attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
                        // 修改动画样式
                        attributes.windowAnimations = com.bigkoo.pickerview.R.style.picker_view_slide_anim;
                        attributes.gravity = Gravity.BOTTOM;
                        dialogWindow.setAttributes(attributes);
                    }
                }
            }
        }
    }

    public abstract void confirm(String label, int position);

    public void cancel() {
    }

    public void setData(List<String> data) {
        pickerView.setPicker(data);
    }

    public void showSinglePickerView() {
        pickerView.show();
    }
}
